<!--  -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="账户名称">
                            <a-input v-model="accountName" placeholder="请输入账户名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' @click="getData">查询</a-button>
                            <a-button style="margin-left: 20px" type='' @click="add">绑定账户</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}" size="small" >
            <div slot="accountType" slot-scope="accountType">
                <div v-for="(item,i) in accountTypeList" :key="item.id">
                    <a-tag v-if="item.id == accountType" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="linkageType" slot-scope="linkageType,record">
                <div v-for="(item,i) in linkageTypeList" :key="item.id">
                    <a-tag v-if="item.id == linkageType" :color="colors[i%13]">{{linkageType != 1 ? item.name : `${item.name}，授权账户：${record.linkageAccounts && record.linkageAccounts.length > 0 ? record.linkageAccounts[0].accountName : '-'}`}}</a-tag>
                </div>
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-popconfirm
                        title="是否解绑当前支付账户?"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="updateBind(record.id,'0')"
                    >
                        <a-button type="link">解绑</a-button>
                    </a-popconfirm>
                </div>
            </div>
        </a-table>

        <a-modal 
            v-model="visible" 
            title="绑定账户"
            @ok="handleOk" 
            @cancel="visible = false">
            <div>
                <a-form-model :model="info" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="开户行" prop="channel">
                        <a-select v-model="info.channel" placeholder="请选择" @change="getAccount">
                            <a-select-option v-for="item in channelList" :key="item" :value="item">{{item}}</a-select-option>
                        </a-select>
                    </a-form-model-item>
                    <a-form-model-item label="账户名称" prop="accountName">
                        <a-select v-model="info.accountName" placeholder="请选择" @change="getAccount">
                            <a-select-option v-for="item in accountNameList" :key="item" :value="item">{{item}}</a-select-option>
                        </a-select>
                    </a-form-model-item>
                    <a-form-model-item label="银行账号" prop="bankAccount">
                        <a-input v-model="info.bankAccount" :disabled="true" allow-clear placeholder="根据开户行和账户名称自动匹配" />
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { getLaborBindAccountList , bindBusinessAccount , getAccountListByBusiness } from '@/api/services/account';
export default {
    props: ['customerBusinessId'],
    data () {
        return {
            accountName: '', // 账户名称
            page: 1,
            size: 10,
            total: 0,
            dataList: [],
            isload: false,
            columns,
            visible: false,
            info: {
                accountId: '',
                channel: undefined,
                accountName: undefined,
                bankAccount: '',
            },
            rules: {
                accountName: [{ required: true, message: '请选择账户名称', trigger: 'blur' }],
                channel: [{ required: true, message: '请选择开户行', trigger: 'blur' }],
                bankAccount: [
                    { required: true, message: '暂无银行账户', trigger: 'blur' },
                ],
            },
            accountNameList: [], // 账户名称列表
            channelList: [], // 开户行列表
            accountTypeList: [
                { id: 1, name: '一般账户'},
                { id: 2, name: '基本账户'},
                { id: 3, name: '一般账户'},
                { id: 4, name: '一般账户'},
            ],
            linkageTypeList: [
                { id: 0, name: '未开通'},
                { id: 1, name: '发起方'},
                { id: 2, name: '授权方'},
            ],
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
        };
    },
    created(){
        this.getAccount();
        this.getData();
    },
    methods: {
        // 获取列表数据
        getData(){
            let _this = this;
            _this.isload = true;
            const params = {
                'accountName': _this.accountName,
                'customerBusinessId': _this.customerBusinessId,
                'pageSize': _this.size,
                'pageIndex': _this.page
            }
            getAccountListByBusiness(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 绑定账号
        add(){
            this.channelList = [];
            this.accountNameList = [];
            this.getAccount();
            this.visible = true
        },
        handleOk(){
            let _this = this
            _this.$refs.ruleForm.validate(valid => {
                if(valid){
                    _this.updateBind(_this.info.accountId,1)
                }
            })
        },
        // 获取可绑定账户
        getAccount(){
            let _this = this
            const params = {
                'accountName': _this.info.accountName,
                'channel': _this.info.channel,
            }
            _this.info.bankAccount = ''
            getLaborBindAccountList(params)
            .then(res=>{
                if(res.success){
                    console.log(_this.info.accountName,'v')
                    if(!_this.info.accountName && !_this.info.channel && res.data.length > 0){
                        console.log('1122')
                        res.data.forEach(item => {
                            _this.channelList.push(item.channel)
                            _this.accountNameList.push(item.accountName)
                        })
                    }else if(_this.info.accountName != '' && _this.info.channel != '' && res.data.length > 0){
                        _this.info.bankAccount = res.data[0].bankAccount
                        _this.info.accountId = res.data[0].id
                    }
                    console.log(_this.channelList,'channelList')
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 绑定活解绑账户
        updateBind(accountId,state){
            let _this = this
            const params = {
                'customerBusinessId': _this.customerBusinessId,
                'accountId': accountId,
                'isBind': state
            }
            bindBusinessAccount(params)
            .then(res=>{
                if(res.success){
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: state == '1' ? '添加成功' : '解绑成功',
                        duration: 8
                    })
                    _this.visible = false;
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        }
    }
}

</script>
<style scoped>

</style>